#include<stdlib.h>
#include<gtk-2.0/gtk/gtk.h>

#define	WDT	600	/*width of the window/drawing area*/
#define HGT	400	/*height of ^^^*/

/*Called each time the widget is resized; including when it is started*/
GdkPixmap *pmap = NULL;
gboolean config_event(GtkWidget* widget, GdkEventConfigure* event) {
	/*If the pixmap already exists is must be destroyed and rebuilt*/
	if(pmap) {
		g_object_unref(pmap);
	}

	/*Create a new pixmap with the same size and color depth as its parent window*/
	pmap  = gdk_pixmap_new(widget->window, widget->allocation.width, widget->allocation.height, -1);

	/*Draw a background onto the pixmap*/
	gdk_draw_rectangle(pmap, widget->style->white_gc, TRUE, 0, 0, widget->allocation.width, widget->allocation.height);

	return TRUE;
}

/*Called each time the widget is uncovered; redraws the pixmap*/
gboolean expose_event(GtkWidget* widget, GdkEventExpose* event) {
	gdk_draw_drawable(widget->window, widget->style->fg_g [gtk_widget_get_state (widget)], pmap, event->area.x, event->area.y, event->area.x, event->area.y, event->area.width, event->area.height);

	return FALSE;
}

gboolean motion_event(GtkWidget* widget, GdkEventMotion* event) {
	int x, y;
	/*Some GTK events come with modifiers which inform the program of simultaneous events*/
	/*These events include: shift, ctrl, modifier keys and mouse buttons*/
	GdkModifierType mods;

	if(event->is_hint) { /*if hint data must be procured w/o event*/
		gdk_window_get_pointer(event->window, &x, &y, &mods);
	} else {
		x = event->x;
		y = event->y;
		state = event->state;
	}

	/*event coordinates and modifiers attained; make use of these here*/

	return TRUE;
}

gboolean btnpress_event(GtkWidget* widget, GdkEventButton* event) {
	/*Code here is entirely dependent on implementation*/
	return TRUE;
}


void quit() { exit(0); } /*terminates the program gracefully*/

int main(int argc, char* argv[]) {
	/*declaring widgets*/
	GtkWidget* window;
	GtkWidget* drawa;
	GtkWidget* vbox; /*for packing widgets; could be an hbox*/

	gtk_init(&argc, &argv);

	/*defining widgets*/
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL); /*window begins as visible to the user*/
	gtk_widget_set_name(window, "ARCSim Dev UI");

	vbox = gtk_vbox_new(FALSE, 0); /*Non-homogenous with no spacing*/
	gtk_container)add(GTK_CONTAINER (window), vbox); /*add the vbox to the window*/_
	gtk_widget_show(vbox);

	drawa = gtk_drawing_area_new();
	gtk_widget_set_size_request(GTK_WIDGET(drawa), WDT, HGT);
	/*gtk_box_pack_start(GTK_BOX(box), GtkWidget widget, gboolean expand, gboolean fill, gint padding);*/
	gtk_box_pack_start(GTK_BOX(vbox), drawa, TRUE, TRUE, 0); /*packs the drawing area into the vbox and expands it to fit the screen with no padding*/
	gtk_widget_show(drawa);

	/*Pixmap signals*/
	g_signal_connect(window, "destroy", G_CALLBACK(quit), NULL);
	g_signal_connect(drawa, "expose_event", G_CALLBACK(expose_event), NULL); /*redraws the pixmap when a part is uncovered*/
	g_signal_connect(drawa, "configure_event", G_CALLBACK(config_event), NULL); /*handles initiation of the drawing area*/

	/*Drawing area events*/

	g_signal_connect(drawa, "motion_notify_event", G_CALLBACK(motion_event), NULL);
	g_signal_connect(drawa, "buttn_press_event"), G_CALLBACK(btnpress_event), NULL);
	/*Set events which are to be handled*/
	/*exposure: widget exposed/uncovered*/
	/*leave notify: pointer leaves widget area*/
	/*button press: key pressed*/
	/*pointer motion: pointer moved*/
	/*pointer motion hint: pointer moved | button clicked*/
	gtk_widget_set_events(drawa, GDK_EXPOSURE_MASK
			 | GDK_LEAVE_NOTIFY_MASK
			 | GDK_BUTTON_PRESS_MASK
			 | GDK_POINTER_MOTION_MASK
			 | GDK_POINTER_MOTION_HINT_MASK);

	/*Finishing up*/
	gtk_widget_show(window);

	gtk_main();

	return 0;
}



